package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.CouponParam;
import com.cskaoyan.bean.bo.ListuserParam;
import com.cskaoyan.bean.pojo.MallCoupon;
import com.cskaoyan.bean.pojo.MallCouponExample;
import com.cskaoyan.bean.pojo.MallCouponUser;
import com.cskaoyan.bean.pojo.MallCouponUserExample;
import com.cskaoyan.mapper.MallCouponMapper;
import com.cskaoyan.mapper.MallCouponUserMapper;
import com.cskaoyan.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author JJ
 * @description CouponService优惠卷接口实现类
 * @time 2021/7/9 11:45
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MallCouponMapper mallCouponMapper;

    @Autowired
    MallCouponUserMapper mallCouponUserMapper;


    @Override
    public BaseData<MallCoupon> listCoupon(CouponParam param) {

        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(), param.getLimit());


        MallCouponExample mallCouponExample = new MallCouponExample();

        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();
        //查询需要设置一个排序和是否删除
        criteria.andDeletedEqualTo(false);
        if (param.getName() != null) {
            criteria.andNameLike("%" +param.getName()+"%");
        }

        if (param.getType() != null) {
            criteria.andTypeEqualTo(param.getType());
        }

        if (param.getStatus() != null) {
            criteria.andStatusEqualTo((int)param.getStatus());
        }
        mallCouponExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallCoupon> mallCouponList = mallCouponMapper.selectByExample(mallCouponExample);

        //total可以使用countByExample，也可以使用PageHelper来获得
        //int total = mallCouponMapper.countByExample(mallCouponExample);
        //在执行查询之后可以获得分页信息,将查询结果放入PageInfo的构造方法里
        PageInfo<MallCoupon> pageInfo = new PageInfo<>(mallCouponList);
        int total = (int) pageInfo.getTotal();

        return BaseData.ok(mallCouponList, total);
    }


    @Override
    public Integer createCoupon(MallCoupon param) {


        //如果type是2则要生成兑换码
        if (param.getType()==2){
            String s = UUID.randomUUID().toString().replace("-", "").toUpperCase();
            String encode = s.substring(0, 8);
            param.setCode(encode);
        }


//        String dateStr =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        Date date = new Date();
        param.setAddTime(date);
        param.setUpdateTime(date);

        //判断优惠券时间类型
        //绝对时间直接赋值
        if (param.getTimeType()==1){
            //自己选日期时没有选应该直接返回错误
            if (param.getStartTime()==null||param.getEndTime()==null){
                return -1;
            }
            int i = mallCouponMapper.insertSelective(param);
            return i;
        }
        if (param.getTimeType()==0){


            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String formatDate = sdf.format(date);


            Calendar calendar = Calendar.getInstance();
            //将现在的时间加上天数得到结束时间
            calendar.add(Calendar.DATE,param.getDays());
            Date time = calendar.getTime();

            String formatTime = sdf.format(time);

            try {
                date = sdf.parse(formatDate);
                time = sdf.parse(formatTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            param.setStartTime(date);
            param.setEndTime(time);

        }


        int i = mallCouponMapper.insertSelective(param);
        return i;
    }

    @Override
    public List<MallCoupon> queryCoupon(MallCoupon param) {
        MallCouponExample mallCouponExample = new MallCouponExample();
        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();

        if (param.getId()!=null){
            criteria.andIdEqualTo(param.getId());
        }
        if (param.getName()!=null){
            criteria.andNameEqualTo(param.getName());
        }
        List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);

        return mallCoupons;
    }


    @Override
    public Integer updateCoupon(MallCoupon param) {
        MallCouponExample mallCouponExample = new MallCouponExample();
        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();

        if (param.getId() != null) {
            criteria.andIdEqualTo(param.getId());
        }


        //如果type是2则要生成兑换码
        if (param.getType()==2){
            String s = UUID.randomUUID().toString().replace("-", "").toUpperCase();
            String encode = s.substring(0, 8);
            param.setCode(encode);
        }



        Date date = new Date();
        param.setAddTime(null);
        param.setUpdateTime(date);

        //判断优惠券时间类型
        //绝对时间直接赋值
//        if (param.getTimeType()==1){
//            int i = mallCouponMapper.updateByExampleSelective(param,mallCouponExample);
//            return i;
//        }
//        if (param.getTimeType()==0){
//            param.setStartTime(date);
//            Calendar calendar = Calendar.getInstance();
//            //将现在的时间加上天数得到结束时间
//            calendar.add(Calendar.DATE,param.getDays());
//            Date time = calendar.getTime();
//            param.setEndTime(time);
//
//        }


        if (param.getTimeType()==1){
            //自己选日期时没有选应该直接返回错误
            if (param.getStartTime()==null||param.getEndTime()==null){
                return -1;
            }
            int i = mallCouponMapper.updateByExampleSelective(param,mallCouponExample);
            return i;

        }
        if (param.getTimeType()==0){

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String formatDate = sdf.format(date);


            Calendar calendar = Calendar.getInstance();
            //将现在的时间加上天数得到结束时间
            calendar.add(Calendar.DATE,param.getDays());
            Date time = calendar.getTime();

            String formatTime = sdf.format(time);

            try {
                date = sdf.parse(formatDate);
                time = sdf.parse(formatTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            param.setStartTime(date);
            param.setEndTime(time);

        }


        int i = mallCouponMapper.updateByExampleSelective(param, mallCouponExample);
        return i;

    }


    @Override
    public Integer deleteCoupon(MallCoupon param) {
        MallCouponExample mallCouponExample = new MallCouponExample();


        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();

        criteria.andIdEqualTo(param.getId());
        //逻辑删除
        param.setDeleted(true);

        int i = mallCouponMapper.updateByExampleSelective(param, mallCouponExample);

        return i;
    }


    @Override
    public MallCoupon readCoupon(Integer id) {
        MallCoupon mallCoupon = mallCouponMapper.selectByPrimaryKey(id);
        return mallCoupon;
    }


    @Override
    public BaseData<MallCouponUser> listuserByCoupon(ListuserParam param) {

        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(), param.getLimit());

        MallCouponUserExample mallCouponUserExample = new MallCouponUserExample();
        MallCouponUserExample.Criteria criteria = mallCouponUserExample.createCriteria();
        //条件为CouponId为传入的id
        criteria.andCouponIdEqualTo(param.getCouponId());


        mallCouponUserExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallCouponUser> mallCouponUsers = mallCouponUserMapper.selectByExample(mallCouponUserExample);

        //total可以使用countByExample，也可以使用PageHelper来获得
        //int total = mallCouponMapper.countByExample(mallCouponExample);
        //在执行查询之后可以获得分页信息,将查询结果放入PageInfo的构造方法里
        PageInfo<MallCouponUser> pageInfo = new PageInfo<>(mallCouponUsers);
        int total = (int) pageInfo.getTotal();

        return BaseData.ok(mallCouponUsers, total);

    }


}
